package com.party.mobile.biz.wechat;


import com.alibaba.fastjson.JSONObject;
import com.party.common.constant.Constant;
import com.party.common.utils.StringUtils;
import com.party.common.utils.wechat.WechatUtils;
import com.party.core.model.wechatPlatform.WechatOpenMember;
import com.party.core.service.wechatPlatform.IWechatOpenMemberService;
import com.party.mobile.web.dto.wechat.input.TokenResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 微信接口令牌service
 * @author wei.li
 * @version 2016/8/3 0003
 */

@Service
public class WechatTokenBizService {

    @Autowired
    IWechatOpenMemberService wechatOpenMemberService;

    //获取接口交互令牌接口地址
    private static String TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET";

    //appid 替换参数
    private static String APPID = "APPID";

    //密钥参数
    private static String APPSECRET = "APPSECRET";

    protected static Logger logger = LoggerFactory.getLogger(WechatTokenBizService.class);

    /**
     * 获取微信接口交互令牌
     * @return 令牌
     */
    public String getToken() throws Exception {
        WechatOpenMember wechatOpenMember = wechatOpenMemberService.getSystem();
        return this.getToken(wechatOpenMember.getAuthorizerAppid(), wechatOpenMember.getSecret());
    }

    /**
     * 获取商户交互令牌
     * @param appid appid
     * @param appsecret appsecret
     * @return 令牌
     */
    public String getToken(String appid, String appsecret) throws Exception {
        String url = TOKEN_URL.replace(APPID,appid).replace(APPSECRET, appsecret);
        logger.info("微信获取接口交互令牌请求数据:{}",url);
        JSONObject response = WechatUtils.httpRequest(url, WechatUtils.GET, null);
        logger.info("微信获取接口交互令牌返回数据:{}", response);
        TokenResponse tokenResponse = JSONObject.toJavaObject(response, TokenResponse.class);
        if (StringUtils.isEmpty(tokenResponse.getAccess_token())) {
            Integer errcode = response.get(Constant.WECHAT_ERR_CODE) != null ? (Integer) response.get(Constant.WECHAT_ERR_CODE) : -1;
            String errmsg = response.get("errmsg") != null ? response.get("errmsg").toString() : "";
            Integer invalidErrorCode = 40164;
            // 40164 没有配置IP白名单
            if (invalidErrorCode.equals(errcode) && errmsg.indexOf("invalid ip") != -1) {
                return null;
            }
            throw new Exception("刷新token异常：" + JSONObject.toJSONString(response));
        }
        return tokenResponse.getAccess_token();
    }
}
